package dao;

import entity.Supplier;
import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SupplierDao extends BaseDao {
    //列表所有用户信息
    public List getAllSuppliers() throws Exception {
        List supplierList = new ArrayList();
        String sql = "select supplierId,supplierName,contactNumber,address" +
                " from supplier";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        //遍历查询结果
        while(resultSet.next()){
            String supplierId = resultSet.getString("supplierId");
            String supplierName = resultSet.getString("supplierName");
            String contactNumber = resultSet.getString("contactNumber");
            String address = resultSet.getString("address");
            Supplier Supplier = new Supplier(supplierId,supplierName,contactNumber,address);
            supplierList.add(Supplier);
        }
        //返回供货商信息列表
        return supplierList;
    }

    //录入供货商信息
    public boolean addSupplier(Supplier supplier) throws Exception {
        String sql = "insert into supplier(supplierId,supplierName,contactNumber,address)" +
                " VALUES(?,?,?,?)";
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, supplier.getSupplierId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, supplier.getSupplierName());
        //设置第二个？占位符的值
        preparedStatement.setString(3, supplier.getContactNumber());
        //设置第三个？占位符的值
        preparedStatement.setString(4, supplier.getAddress());
        //设置第四个？占位符的值
        int num = preparedStatement.executeUpdate();//执行插入sql语句
        if (num==1)
        {
            return true;//执行成功
        }else {
            return false;//执行失败
        }
    }

    //修改供货商信息
    public boolean updateSupplier(Supplier supplier) {
        int i = 0;
        try{
            Connection connection = getConnection();//获取连接
            String updatesql = "update supplier set supplierName=?,"
                    +"contactNumber=?,"
                    +"address=?"
                    +" where supplierId=?";
            PreparedStatement preparedStatement = connection.prepareStatement(updatesql);
            //设置？占位符的值
            preparedStatement.setString(1,supplier.getSupplierName());
            preparedStatement.setString(2,supplier.getContactNumber());
            preparedStatement.setString(3,supplier.getAddress());
            preparedStatement.setString(4,supplier.getSupplierId());
            //执行修改sql语句
            i = preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        if (i == 1){
            return true;//执行成功
        }else {
            return false;//执行失败
        }
    }

    //删除供货商信息
    public String deleteSupplier(Supplier supplier) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from supplier where supplierId=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,supplier.getSupplierId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        SupplierDao supplierDao = new SupplierDao();
        System.out.println(supplierDao.getAllSuppliers());
    }
}
